package fr.inserm.um915.bdibd.model;

import java.awt.Point;

import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;


public class Individual implements Comparable<Individual>
	{
	public int id;
	public String name;
	public String family;
	public String fatherId;
	public String motherId;
	public int gender;
	public int status;
	public Point pedPt=new Point(0,0);
	
	static class Binding extends TupleBinding<Individual>
		{
		@Override
		public Individual entryToObject(TupleInput in) {
			Individual o=new Individual();
			o.id=in.readInt();
			o.name=in.readString();
			o.family=in.readString();
			o.fatherId=in.readString();
			if(o.fatherId.length()==0) o.fatherId=null;
			o.motherId=in.readString();
			if(o.motherId.length()==0) o.motherId=null;
			o.gender=in.readInt();
			o.status=in.readInt();
			o.pedPt.x=in.readInt();
			o.pedPt.y=in.readInt();
			return o;
			}
		
		@Override
		public void objectToEntry(Individual o, TupleOutput out)
			{
			out.writeInt(o.id);
			out.writeString(o.name);
			out.writeString(o.family);
			out.writeString(o.hasFather()?o.fatherId:"");
			out.writeString(o.hasMother()?o.motherId:"");
			out.writeInt(o.gender);
			out.writeInt(o.status);
			out.writeInt(o.pedPt.x);
			out.writeInt(o.pedPt.y);
			}
		}
	
	static public Binding BINDING=new Binding();
	
	@Override
	public int hashCode()
		{
		final int prime = 31;
		int result = 1;
		result = prime * result +family.hashCode();
		result = prime * result +name.hashCode();
		return result;
		}

	@Override
	public int compareTo(Individual o) {
		int i=family.compareTo(o.family);
		if(i!=0) return i;
		return name.compareTo(o.name);
		}
	
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Individual other = (Individual) obj;
		return 	family.equals(other.family) &&
				name.equals(other.name);
		}

	public int getId()
		{
		return id;
		}
	
	public String getName()
		{
		return name;
		}
	
	public String getFamily() {
		return family;
		}
	
	public boolean hasFather()
		{
		return !(this.fatherId==null || fatherId.isEmpty() || fatherId.equals("0"));
		}
	
	public boolean hasMother()
		{
		return !(this.motherId==null || motherId.isEmpty() || motherId.equals("0"));
		}
	
	public String getFatherId()
		{
		return fatherId;
		}
	
	public String getMotherId()
		{
		return motherId;
		}
	
	public int getGender() {
		return gender;
		}
	
	public int getStatus()
		{
		return status;
		}
	
	public int getX()
		{
		return this.pedPt.x;
		}
	
	public int getY()
		{
		return this.pedPt.y;
		}
	
	
	@Override
	public String toString() {
		return family+" "+name;
		}
	
	
	
	}
